package club.kingyin.easycache.proxy;

import club.kingyin.easycache.key.InvokePostProcess;
import club.kingyin.easycache.cache.ProxyMethodCache;
import club.kingyin.easycache.method.CacheMethod;
import club.kingyin.easycache.proxy.adapter.CacheMethodKeyAdapter;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class CglibObjectFactory {

    public static Object proxyObjectFactory(Object target, ProxyMethodCache cache,
                                            InvokePostProcess<CacheMethod>[] postProcesses,
                                            CacheMethodKeyAdapter adapter
                                            ) {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(target);
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
        ProxyEasyCache proxyEasyCache = new ProxyEasyCache(cache, adapter);
        proxyEasyCache.setInvokePostProcesses(postProcesses);
        defaultPointcutAdvisor.setAdvice(proxyEasyCache);
        proxyFactory.addAdvisor(defaultPointcutAdvisor);
        return proxyFactory.getProxy();
    }

}
